<?php
/**
 * Created by PhpStorm.
 * User: 夏浩文
 * Date: 2017/6/14
 * Time: 15:01
 */
namespace app\hongyanche\controller;

class CouponAdmin
{
    private $auth;
    public function __construct()
    {
        model('product')->demand('coupon');
        if (!model('admin')->is('coupon')['state']) {
            $res = array('state' => 0, 'errormsg' => 'authority incorrect', 'errorcode' => '');
            $this->auth = $res;
        }else{
            $this->auth = array('state'=>1);
        }
    }
    //获取标签列表
    public function classId_rank()
    {
        $config = getConfig('coupon');
        foreach ($config as $k =>$v) {
            $rank[$v['id']]['way'] = $k;
            $rank[$v['id']]['id'] = $v['id'];
            $rank[$v['id']]['name'] = $v['content'];
        }
        $res = array('state'=>1,'data'=>array('rank'=>$rank));
        return json($res);
    }
    //创建优惠券
    public function add()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $data['way'] = input('way');
        $data['name'] = input('name');
        $data['describe'] = input('describe');
        $data['discount'] = input('discount');
        $data['full_price'] = input('full_price');
        $data['amount'] = input('amount');
        $data['start_time'] = input('start_time');
        $data['end_time'] = input('end_time');
        $data['target'] = input('target');
        $data['obtain'] = input('obtain');
        $data['obtain_amount'] = input('obtain_amount');
        $res = model('coupon')->add($data);
        return json($res);
    }
    //场地优惠券
    public function modify()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);;
        $coupon_id = input('coupon_id');
        $key = input('key');
        $content = input('content');
        $res = model('product')->modify($coupon_id, $key, $content);
        return json($res);
    }
    //删除优惠券
    public function remove()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $brand_id = input('coupon_id');
        $res = model('product')->remove($brand_id);
        return json($res);
    }
    //优惠券信息展示
    public function getList()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $filer['page'] = input('page');
        $filer['maxPerPage'] = input('maxPerPage');
        $filer['keywords'] = input('keywords');
        $filer['select'] = input('way');
        $res = model('coupon')->getList($filer);
        return json($res);
    }
    //开放、关闭
    public function release()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $brand_id = input('coupon_id');
        $release = input('release');
        $res = model('product')->release($brand_id, $release);
        return json($res);
    }
    //分配优惠券给用户
    public function allotCoupon()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $user_id = input('user_id');
        $brand_id = input('mall_id');
        $coupon_id = input('coupon_id');
        $res = model('coupon')->allotCoupon($user_id,$brand_id,$coupon_id);
        return json($res);
    }

}
